package com.car.serve.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import com.car.serve.Param.addServeProjectParam;
import com.car.serve.Param.upServeProjectParam;
import com.car.serve.dto.ServeProjectDto;
import com.car.serve.entity.InfoProject;
import com.car.serve.entity.ServeProject;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageServeProjectForm;
import com.car.serve.form.addServeProjectForm;
import com.car.serve.form.upServeProjectForm;
import com.car.serve.service.InfoProjectService;
import com.car.serve.service.ServeProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/serveProject")
@Api(tags = "服务项目信息")
public class ServeProjectController {
    @Resource
    private ServeProjectService serveProjectService;
    @Resource
    private InfoProjectService infoProjectService;

    @PostMapping("/addServeProject.pt")
    @ApiOperation(value = "新增服务项目")
    public Result addServeProject(@RequestBody addServeProjectForm form)throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        addServeProjectParam param = BeanUtil.toBean(form, addServeProjectParam.class);
        param.setAdminId(adminId);
        serveProjectService.addServeProject(param);
        return Result.getResult().setMsg("新增服务项目成功");

    }

    @PostMapping("/upServeProject.pt")
    @ApiOperation(value = "修改服务项目")
    public Result upServeProject(@RequestBody upServeProjectForm form)throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        upServeProjectParam param = BeanUtil.toBean(form, upServeProjectParam.class);
        param.setAdminId(adminId);
        serveProjectService.upServeProject(param);
        return Result.getResult().setMsg("修改服务项目成功");
    }

    @PostMapping("/delServeProject")
    @ApiOperation(value = "删除服务项目")
    public Result delServeProject(Integer projectId)throws Exception{
        serveProjectService.delServeProject(projectId);
        return Result.getResult().setMsg("删除服务项目成功");
    }

    @PostMapping("/disableServeProject")
    @ApiOperation(value = "禁用项目")
    public Result disableServeProject(Integer projectId)throws Exception{
        serveProjectService.disableServeProject(projectId);
        return Result.getResult().setMsg("禁用项目成功");
    }

    @PostMapping("/enableServeProject")
    @ApiOperation(value = "启用项目")
    public Result enableServeProject(Integer projectId)throws Exception{
        serveProjectService.enableServeProject(projectId);
        return Result.getResult().setMsg("启用项目成功");
    }

    @GetMapping("/findByIdServeProject.pt")
    @ApiOperation(value = "根据ID查询项目详情")
    public Result<ServeProject> findByIdServeProject(Integer projectId)throws Exception{
        ServeProject serveProject = serveProjectService.getById(projectId);
        if (serveProject==null)throw new ServeException(3000,"不存在该项目");
        return Result.getResult(ServeProject.class).setMsg("根据ID查询项目详情成功").setData(serveProject);
    }
    @PostMapping("/findPageServeProject.pt")
    @ApiOperation(value = "分页查询")
    public Result findPageServeProject(@RequestBody PageServeProjectForm form)throws Exception{
        Page<ServeProject> pageServeProject = serveProjectService.findPageServeProject(form);
        return Result.getResult().setMsg("分页查询成功").setData(pageServeProject);
    }
    @GetMapping("/findServeProjectListByServeId/{serveId}.ty")
    @ApiOperation(value = "调用")
    public Result<List<ServeProjectDto>> findServeProjectListByServeId(@PathVariable("serveId") Integer serveId){
        List<InfoProject> infoProjects = infoProjectService.list(new QueryWrapper<InfoProject>().eq("serve_id", serveId));
        List<Integer> collect = infoProjects.stream().sorted(Comparator.comparing(InfoProject::getSortValue)).map(InfoProject::getProjectId).collect(Collectors.toList());
        List<ServeProject> serveProjects = serveProjectService.list(new QueryWrapper<ServeProject>().in("project_id", collect));
        List<ServeProjectDto> serveProjectDtos = BeanUtil.copyToList(serveProjects, ServeProjectDto.class);
        ArrayList<ServeProjectDto> serveProjectDtos1 = new ArrayList<>();
        for (InfoProject infoProject : infoProjects) {
            for (ServeProjectDto serveProjectDto : serveProjectDtos) {
                if (infoProject.getProjectId().equals(serveProjectDto.getProjectId())) {
                    serveProjectDto.setSortValue(infoProject.getSortValue());
                    serveProjectDtos1.add(serveProjectDto);
                }
            }
        }
        System.out.println(serveProjectDtos1);
        return Result.getListResult(ServeProjectDto.class).setMsg("查询成功").setData(serveProjectDtos1);
    }

    @GetMapping("/findServeProjectListByIds.ty")
    @ApiOperation(value = "调用")
    public Result<List<ServeProjectDto>> findServeProjectListByServeId(@RequestParam("serveProjectIds") Set<Integer> serveProjectIds){
        List<ServeProject> serveProjects = serveProjectService.listByIds(serveProjectIds);
        return Result.getListResult(ServeProjectDto.class).setMsg("查询成功").setData(BeanUtil.copyToList(serveProjects,ServeProjectDto.class));
    }
}

